package cs6238.common;

import java.nio.ByteBuffer;
import java.util.UUID;

/**
 * Simple datastructure for sending information on Files from server to client
 */
public class FileInfo {

	private String _clientFilename;
	private UUID _fileId;
	private String _owningHost;
	
	public FileInfo(){}
	
	public FileInfo(String filename, UUID clientFileId, String owningHost){
		_clientFilename = filename;
		_fileId = clientFileId;
		_owningHost = owningHost;
	}

	public String toString(){
		return _clientFilename + " \t" + _fileId.toString() + " \t" + _owningHost;
	}
	
	public byte[] serialize(){
		byte[] filenameBytes = ByteArray.writeString(_clientFilename);
		byte[] owningHostBytes = ByteArray.writeString(_owningHost);
		
		ByteBuffer bb = ByteBuffer.allocate(filenameBytes.length + 16 + owningHostBytes.length);
		bb.put(filenameBytes);
		bb.putLong(_fileId.getMostSignificantBits());
		bb.putLong(_fileId.getLeastSignificantBits());
		bb.put(owningHostBytes);
		
		return bb.array();
	}
	
	public void deserialize(ByteBuffer bb){
		_clientFilename = ByteArray.readString(bb);
		long mostSig = bb.getLong();
		long leastSig = bb.getLong();
		_fileId = new UUID(mostSig, leastSig);
		_owningHost = ByteArray.readString(bb);
	}
	
	public void setFilename(String filename){
		_clientFilename = filename;
	}
	
	public String getFilename(){
		return _clientFilename;
	}
	
	public void setFileId(UUID fileId){
		_fileId = fileId;
	}
	
	public UUID getFileId(){
		return _fileId;
	}
	
	public String getOwningHost(){
		return _owningHost;
	}
	
	public void setOwningHost(String owningHost){
		_owningHost = owningHost;
	}
}
